//
//  YJMSendRequest.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/30.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "YJMSendRequest.h"
#import "SaintiNetWork.h"
#import "AES128Util.h"
#import "NSString+Expand.h"
#import "NSUDManager.h"
@implementation YJMSendRequest
/*
 方法说明:
 用户登录
 
 参数说明:
 空
 
 返回结果:
 NSDictionary *result；block的返回结果，这个可以自定义，可以是数组，字典，也可以是一个实例类，建议实例类，这样用起来比较方便，代码易读。
 */
+ (NSURLSessionDataTask *)loginWithTel:(NSString *)tel smsCode:(NSString *)smsCode result:(void (^)(NSDictionary *result, NSError *error))block{
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setObject:tel forKey:@"tel"];
    [dic setObject:smsCode forKey:@"smsCode"];
    
    NSString *strAES128 = [AES128Util AES128Encrypt:[NSString dictionaryToJson:dic]];
    return [[SaintiNetWork sharedClient]POST:@"user/login" parameters:@{@"data":strAES128} progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON){
        if (block) {//添加block返回

            block(JSON, nil);
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error){
        if (block) {
            block(nil, error);
        }
    }];
    
}
/** 发送验证码 */
+ (NSURLSessionDataTask *)sendVerificationCodeWithTel:(NSString *)tel result:(void (^)(NSDictionary *result, NSError *error))block {
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setObject:tel forKey:@"tel"];
    NSString *strAES128 = [AES128Util AES128Encrypt:[NSString dictionaryToJson:dic]];
    return [[SaintiNetWork sharedClient]POST:@"common/sendVerificationCode" parameters:@{@"data":strAES128} progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON){
        if (block) {//添加block返回
            block(JSON, nil);
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error){
        if (block) {
            block(nil, error);
        }
    }];
}
/** 更新用户信息 */
+ (NSURLSessionDataTask *)updateUserInfoWithHeadImg:(NSString *)headImg nickName:(NSString *)nickName result:(void (^)(NSDictionary *result, NSError *error))block {
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    if (headImg.length > 0) {
        [dic setObject:headImg forKey:@"headImg"];
    }
    [dic setObject:nickName forKey:@"nickName"];
    
    NSString *strAES128 = [AES128Util AES128Encrypt:[NSString dictionaryToJson:dic]];
    return [[SaintiNetWork sharedClient]POST:@"user/updateUserInfo" parameters:@{@"data":strAES128} progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON){
        if (block) {//添加block返回
            block(JSON, nil);
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error){
        if (block) {
            block(nil, error);
        }
    }];
}
/** 更新用户手机号*/
+ (NSURLSessionDataTask *)updateUserTelWithTel:(NSString *)tel smsCode:(NSString *)smsCode result:(void (^)(NSDictionary *result, NSError *error))block {
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setObject:tel forKey:@"tel"];
    [dic setObject:smsCode forKey:@"smsCode"];
    
    NSString *strAES128 = [AES128Util AES128Encrypt:[NSString dictionaryToJson:dic]];
    return [[SaintiNetWork sharedClient]POST:@"user/updateUserTel" parameters:@{@"data":strAES128} progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON){
        if (block) {//添加block返回
            block(JSON, nil);
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error){
        if (block) {
            block(nil, error);
        }
    }];
}
/** 提现*/
+ (NSURLSessionDataTask *)withdrawDepositWithAmount:(NSString *)amount alipayAccount:(NSString *)alipayAccount result:(void (^)(NSDictionary *result, NSError *error))block {
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setObject:alipayAccount forKey:@"alipayAccount"];
    [dic setObject:amount forKey:@"amount"];
    
    NSString *strAES128 = [AES128Util AES128Encrypt:[NSString dictionaryToJson:dic]];
    return [[SaintiNetWork sharedClient]POST:@"user/withdrawDeposit" parameters:@{@"data":strAES128} progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON){
        if (block) {//添加block返回
            block(JSON, nil);
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error){
        if (block) {
            block(nil, error);
        }
    }];
}
/** 校验用户是否存在支付密码*/
+ (NSURLSessionDataTask *)checkUserHasPayPwdResult:(void (^)(NSDictionary *result, NSError *error))block {
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    NSString *strAES128 = [AES128Util AES128Encrypt:[NSString dictionaryToJson:dic]];
    return [[SaintiNetWork sharedClient]POST:@"user/checkUserHasPayPwd" parameters:@{@"data":strAES128} progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON){
        if (block) {//添加block返回
            block(JSON, nil);
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error){
        if (block) {
            block(nil, error);
        }
    }];
}
/** 更新用户支付密码*/
+ (NSURLSessionDataTask *)updatePayPwdWithAmount:(NSString *)amount alipayAccount:(NSString *)alipayAccount result:(void (^)(NSDictionary *result, NSError *error))block {
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setObject:alipayAccount forKey:@"alipayAccount"];
    [dic setObject:amount forKey:@"amount"];
    
    NSString *strAES128 = [AES128Util AES128Encrypt:[NSString dictionaryToJson:dic]];
    return [[SaintiNetWork sharedClient]POST:@"user/updatePayPwd" parameters:@{@"data":strAES128} progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON){
        if (block) {//添加block返回
            block(JSON, nil);
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error){
        if (block) {
            block(nil, error);
        }
    }];
}
/*
 方法描述:
 统一请求接口
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
+ (NSURLSessionDataTask *)postDataWithParameters:(NSDictionary *)parameters url:(NSString *)url  result:(void (^)(NSDictionary *result, NSError *error))block {
    

    
    NSString *strAES128 = [AES128Util AES128Encrypt:[NSString dictionaryToJson:parameters]];
    return [[SaintiNetWork sharedClient]POST:url parameters:@{@"data":strAES128} progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON){
        if (block) {//添加block返回
            block(JSON, nil);
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error){
        if (block) {
            block(nil, error);
        }
    }];
}
#pragma mark ------七牛图片上传相关
/*
 方法说明:
 获取七牛上传token
 
 参数说明:
 空
 
 返回结果:
 NSDictionary *result；block的返回结果，这个可以自定义，可以是数组，字典，也可以是一个实例类，建议实例类，这样用起来比较方便，代码易读。
 */
+ (NSURLSessionDataTask *)getQiuNiuUpTokenResult:(void (^)(NSDictionary *result, NSError *error))block{
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    NSString *strAES128 = [AES128Util AES128Encrypt:[NSString dictionaryToJson:dic]];
    return [[SaintiNetWork sharedClient]POST:@"common/getQiuNiuUpToken" parameters:@{@"data":strAES128} progress:nil success:^(NSURLSessionDataTask * __unused task, id JSON){
        if (block) {//添加block返回
            NSDictionary *result = [CommonUtils decryptDicWithResult:JSON];
            if([result[@"result"] integerValue] == 3)
            {
//                KUSERDEFAULTS;
//                [defaults removeObjectForKey:@"UserInfo"];
//                [defaults removeObjectForKey:@"loginOK"];
//                [defaults synchronize];
//                [((AppDelegate *)[[UIApplication sharedApplication] delegate]) gotoLogin];
//                [CommonUtils showToast:@"您的账号已经在其他地方登录！"];
            }else{
                block(JSON, nil);
            }
        }
    }failure:^(NSURLSessionDataTask *task, NSError *error){
        if (block) {
            block(nil, error);
        }
    }];
    
}
@end

